package org.jeecg.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.vo.LeaseContractRestVo;
import org.jeecg.modules.api.vo.param.QueryLeaseContractParamVo;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.entity.CustomerInfo;
import org.jeecg.modules.zcgl.entity.LeaseContractInfo;
import org.jeecg.modules.zcgl.service.ICustomerInfoService;
import org.jeecg.modules.zcgl.service.ILeaseContractInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags="三峡银行接口-合同信息信息DEMO")
@RestController
@RequestMapping("/apiDemo/bank/contract")
@Slf4j
public class leaseContractApiControllerBack {

    @Autowired
    private ILeaseContractInfoService iLeaseContractInfoService;
    @Autowired
    private ICustomerInfoService iCustomerInfoService;
    @Autowired
    private ISysDepartService sysDepartService;


    @ApiOperation(value="获取合同信息", notes="获取合同信息")
    @PostMapping("/getContractListById")
    public Result<List<LeaseContractRestVo>> queryLeaseContractInfo(@RequestBody @Validated QueryLeaseContractParamVo vo){

        try{

            CustomerInfo customerInfo=iCustomerInfoService.getById(vo.getCustomerId());
            if(customerInfo==null){
                return Result.error("用户不存在！");
            }


            SysDepart sysDepart = sysDepartService.selectCompanyByName(vo.getRecordedCompanyName());
            if (sysDepart == null) {
                return Result.error("公司不存在！");
            }

            //查询客合同
            LambdaQueryWrapper<LeaseContractInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LeaseContractInfo::getCustomerId,vo.getCustomerId());
            queryWrapper.eq(LeaseContractInfo::getRentCompanyId,sysDepart.getId());
            List<LeaseContractInfo> leaseContractInfoList= iLeaseContractInfoService.list(queryWrapper);

            List<LeaseContractRestVo> leaseContractRestVoList=new ArrayList<LeaseContractRestVo>();
            for (LeaseContractInfo l :leaseContractInfoList) {
                LeaseContractRestVo LeaseContractRestVo=new LeaseContractRestVo();
                LeaseContractRestVo.setContractNum(l.getContractNum());
                leaseContractRestVoList.add(LeaseContractRestVo);
            }
            return Result.ok(leaseContractRestVoList);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }






}
